//
//  ContainerViewController.m
//  ShangHaiTuJI
//
//  Created by xiaoxiangtuji on 2017/7/29.
//  Copyright © 2017年 xiaoxiangtuji. All rights reserved.
//

#import "ContainerViewController.h"
#import "CompetionTopView.h"
#import "CompetionInformViewController.h"
#import "CompetionReviewViewController.h"
#import "UIScrollView+Pages.h"
#import "MyMatchRecordViewController.h"
@interface ContainerViewController ()<UIScrollViewDelegate,CompetionDelegate,UIViewControllerTransitioningDelegate>

@property (nonatomic,strong) UIScrollView *scrollView;

@end

@implementation ContainerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    rightBtn.frame = CGRectMake(WIDTH - 30, 6, 80, 30);
    [rightBtn setTitle:@"我的赛事" forState:UIControlStateNormal];
    [rightBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    rightBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    [rightBtn addTarget:self action:@selector(GoToMymatch) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *flexSpac = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:self action:nil];
    flexSpac.width = -10;
    UIBarButtonItem *rightBtnItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
    self.navigationItem.rightBarButtonItems = @[flexSpac,rightBtnItem];
    CompetionTopView *titleView = [[CompetionTopView alloc] initWithFrame:CGRectMake(0, 0, scaleLeftAndRight(200),30)];
    titleView.layer.cornerRadius = 15;
    titleView.layer.masksToBounds = YES;
    titleView.delegate = self;
    self.navigationItem.titleView = titleView;
    [self.view addSubview:self.scrollView];
    CompetionInformViewController *inforVC = [[CompetionInformViewController alloc] init];
    [self addChildViewController:inforVC];
    CompetionReviewViewController *reviewVC = [[CompetionReviewViewController alloc] init];
    [self addChildViewController:reviewVC];
    inforVC.view.frame = CGRectMake(0, 0, WIDTH, HEIGHT);
    [self.scrollView addSubview:inforVC.view];
    reviewVC.view.frame = CGRectMake(WIDTH, 0, WIDTH, HEIGHT);
    [self.scrollView addSubview:reviewVC.view];
//    [self createNavagationBackBtn];
}
-(void)createNavagationBackBtn{
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    backBtn.frame = CGRectMake(0, 6 , 67, 23);
    UIImageView *backImg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"navigator_btn_back"]];
    backImg.frame = CGRectMake(-10, 10, 22, 22);
    [backBtn addSubview:backImg];
    [backImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(backBtn.mas_left).offset(-10);
        make.centerY.equalTo(backBtn.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(22, 22));
    }];
    UILabel *backText = [[UILabel alloc] initWithFrame:CGRectMake(12, 10, 65, 22)];
    backText.font = [UIFont systemFontOfSize:17];
    [backText setBackgroundColor:[UIColor clearColor]];
    [backText setTextColor:[UIColor whiteColor]];
    [backBtn addSubview:backText];
    [backText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(backImg.mas_right).offset(2);
        make.centerY.equalTo(backBtn.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(65, 22));
    }];
    [backBtn addTarget:self action:@selector(leftBarButtonItemPressed:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
    [self.navigationItem setLeftBarButtonItem:leftButton];
    
}
-(void)leftBarButtonItemPressed:(UIButton *)btn{
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)GoToMymatch{
    MyMatchRecordViewController *mycompetionVc = [[MyMatchRecordViewController alloc] init];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:mycompetionVc];
    nav.transitioningDelegate = self;
    nav.modalPresentationStyle = UIModalPresentationCustom;
    [self presentViewController:nav animated:YES completion:nil];
}
- (void)selectedGroup:(NSInteger)type {
    if (type == 1) {
        [_scrollView setPageX:0 animated:NO];
    }else{
        [_scrollView setPageX:1 animated:NO];
    }
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    if (self.scrollView == scrollView) {
        NSInteger page = scrollView.contentOffset.x / WIDTH;
        [((CompetionTopView *)self.navigationItem.titleView) switchToTab:page + 1];
    }
}

- (UIScrollView *)scrollView {
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc] init];
        _scrollView.frame = self.view.bounds;
        _scrollView.pagingEnabled = YES;
        _scrollView.delegate = self;
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.showsVerticalScrollIndicator = NO;
        _scrollView.bounces = NO;
        _scrollView.contentSize = CGSizeMake(WIDTH * 2, HEIGHT - 64);
    }
    return _scrollView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
